package utils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import bean.BlueBean;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

public class BlueUtils2 {
	// 蓝牙适配器
	private BluetoothAdapter mBluetoothAdapter;
	// 搜索状态的标示
	private boolean mScanning = true;
	// 蓝牙适配器List
	// 上下文
	private Context context;
	// 单例模式
	private static BlueUtils2 blueUtils;
	// 蓝牙的回调地址
	private BluetoothAdapter.LeScanCallback mLesanCall;
	// 扫描执行回调
	// private BlueUddtils.Callback callback;
	private Callbacks2 callback2;
	private List<BlueBean> beanList;
	private List<BluetoothDevice> list;

	// 单例模式
	public static BlueUtils2 getBlueUtils() {
		if (blueUtils == null) {
			blueUtils = new BlueUtils2();
		}
		return blueUtils;
	}

	/***
	 * 初始化蓝牙的一些信息
	 */
	public void getInitialization(Context context) {
		this.context = context;
		// 初始化蓝牙适配器
		BluetoothManager bluetoothManager = (BluetoothManager) context
				.getSystemService(Context.BLUETOOTH_SERVICE);
		// 初始化蓝牙
		mBluetoothAdapter = bluetoothManager.getAdapter();
		
		// 初始化List
		beanList = new ArrayList<BlueBean>();
		list = new ArrayList<BluetoothDevice>();
		// 实例化蓝牙回调
		mLesanCall = new BluetoothAdapter.LeScanCallback() {
			private String deviceName;

			@Override
			public void onLeScan(BluetoothDevice bluetoothDevice, int i,
					byte[] bytes) {
				// 返回三个对象 分类别是 蓝牙对象 蓝牙信号强度 以及蓝牙的广播包
				// 封装需要的数据到javaBean
				if (!list.contains(bluetoothDevice) && bluetoothDevice!=null) {
					list.add(bluetoothDevice);
					BlueBean blueBean = new BlueBean();
//					final BleAdvertisedData badata = parseAdertisedData(bytes);
//					if (bluetoothDevice != null) {
//						deviceName = bluetoothDevice.getName();
//						if (deviceName == null) {
//							deviceName = badata.getName();
//						} else {
//							deviceName = bluetoothDevice.getName();
//						}
//					}
					
					deviceName = bluetoothDevice.getName();
					if (deviceName == null) {
						deviceName = "未知";
					}
					
					blueBean.setBlueName(deviceName);
					Log.d("cai", "blue name ="+deviceName);
					blueBean.setBlueMac(bluetoothDevice.getAddress());
					blueBean.setBlueType(bluetoothDevice.getBluetoothClass()
							.getDeviceClass());
					beanList.add(blueBean);
					// 接口回调
					callback2.CallbackList(beanList);
				}

			}
		};
	}

	/**
	 * 开启蓝牙
	 */
	public void startBlue() {
		if (mScanning) {
			mScanning = false;
			// 开始扫描并设置回调
			mBluetoothAdapter.startLeScan(mLesanCall);
		}
	}

	/**
	 * 停止蓝牙扫描
	 */
	public void stopBlue() {
		if (!mScanning) {
			// 结束蓝牙扫描
			mBluetoothAdapter.stopLeScan(mLesanCall);
		}
	}

	/**
	 * 接口回调
	 */
	public interface Callbacks2 {
		void CallbackList(List<BlueBean> beanList);
	}

	/**
	 * 设置接口回调
	 * 
	 * @param callback
	 *            自身
	 */
	public void setCallback2(Callbacks2 callback2) {
		this.callback2 = callback2;
	}

	/**
	 * 判断是否支持蓝牙
	 * 
	 * @return
	 */
	public boolean isSupportBlue() {
		if (!context.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_BLUETOOTH_LE)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 返回蓝牙对象
	 * 
	 * @return
	 */
	public BluetoothAdapter getmBluetoothAdapter() {
		return mBluetoothAdapter;
	}

	public static BleAdvertisedData parseAdertisedData(byte[] advertisedData) {
		List<UUID> uuids = new ArrayList<UUID>();
		String name = null;
		if (advertisedData == null) {
			return new BleAdvertisedData(uuids, name);
		}

		ByteBuffer buffer = ByteBuffer.wrap(advertisedData).order(
				ByteOrder.LITTLE_ENDIAN);
		while (buffer.remaining() > 2) {
			byte length = buffer.get();
			if (length == 0)
				break;

			byte type = buffer.get();
			switch (type) {
			case 0x02: // Partial list of 16-bit UUIDs
			case 0x03: // Complete list of 16-bit UUIDs
				while (length >= 2) {
					uuids.add(UUID.fromString(String.format(
							"%08x-0000-1000-8000-00805f9b34fb",
							buffer.getShort())));
					length -= 2;
				}
				break;
			case 0x06: // Partial list of 128-bit UUIDs
			case 0x07: // Complete list of 128-bit UUIDs
				while (length >= 16) {
					long lsb = buffer.getLong();
					long msb = buffer.getLong();
					uuids.add(new UUID(msb, lsb));
					length -= 16;
				}
				break;
			case 0x09:
				byte[] nameBytes = new byte[length - 1];
				buffer.get(nameBytes);
				try {
					name = new String(nameBytes, "utf-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				break;
			default:
				buffer.position(buffer.position() + length - 1);
				break;
			}
		}
		return new BleAdvertisedData(uuids, name);
	}

	private String getStyleContent(BluetoothDevice device) {
		int styleMajor = device.getBluetoothClass().getMajorDeviceClass();
		String content = "未知....";
		switch (styleMajor) {
		case BluetoothClass.Device.Major.AUDIO_VIDEO:// 音频设备
			content = "音配设备";
			break;
		case BluetoothClass.Device.Major.COMPUTER:// 电脑
			content = "电脑";
			break;
		case BluetoothClass.Device.Major.HEALTH:// 健康状况
			content = "健康状况";
			break;
		case BluetoothClass.Device.Major.IMAGING:// 镜像，映像
			content = "镜像";
			break;
		case BluetoothClass.Device.Major.MISC:// 麦克风
			content = "麦克风";
			break;
		case BluetoothClass.Device.Major.NETWORKING:// 网络
			content = "网络";
			break;
		case BluetoothClass.Device.Major.PERIPHERAL:// 外部设备
			content = "外部设备";
			break;
		case BluetoothClass.Device.Major.PHONE:// 电话
			content = "电话";
			break;
		case BluetoothClass.Device.Major.TOY:// 玩具
			content = "玩具";
			break;
		case BluetoothClass.Device.Major.UNCATEGORIZED:// 未知的
			content = "未知的";
			break;
		case BluetoothClass.Device.Major.WEARABLE:// 穿戴设备
			content = "穿戴设备";
			break;
		}
		return content;
	}

	public static String getDeviceType(BluetoothDevice device) {
		int a = device.getBluetoothClass().getDeviceClass();
		String name = null;
		switch (a) {
		case BluetoothClass.Device.AUDIO_VIDEO_CAMCORDER:// 录像机
			name = "录像机";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
			name = "车载设备";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
			name = "蓝牙耳机";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_LOUDSPEAKER:
			name = "扬声器";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_MICROPHONE:
			name = "麦克风";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_PORTABLE_AUDIO:
			name = "打印机";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_SET_TOP_BOX:
			name = "BOX";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_UNCATEGORIZED:
			name = "未知的";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_VCR:
			name = "录像机";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_CAMERA:
			name = "照相机录像机";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_CONFERENCING:
			name = "conferencing";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER:
			name = "显示器和扬声器";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_GAMING_TOY:
			name = "游戏";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_MONITOR:
			name = "显示器";
			break;
		case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET:
			name = "可穿戴设备";
			break;
		case BluetoothClass.Device.PHONE_CELLULAR:
			name = "手机";
			break;
		case BluetoothClass.Device.PHONE_CORDLESS:
			name = "无线电设备";
			break;
		case BluetoothClass.Device.PHONE_ISDN:
			name = "手机服务数据网";
			break;
		case BluetoothClass.Device.PHONE_MODEM_OR_GATEWAY:
			name = "手机调节器";
			break;
		case BluetoothClass.Device.PHONE_SMART:
			name = "手机卫星";
			break;
		case BluetoothClass.Device.PHONE_UNCATEGORIZED:
			name = "未知手机";
			break;
		case BluetoothClass.Device.WEARABLE_GLASSES:
			name = "可穿戴眼睛";
			break;
		case BluetoothClass.Device.WEARABLE_HELMET:
			name = "可穿戴头盔";
			break;
		case BluetoothClass.Device.WEARABLE_JACKET:
			name = "可穿戴上衣";
			break;
		case BluetoothClass.Device.WEARABLE_PAGER:
			name = "客串点寻呼机";
			break;
		case BluetoothClass.Device.WEARABLE_UNCATEGORIZED:
			name = "未知的可穿戴设备";
			break;
		case BluetoothClass.Device.WEARABLE_WRIST_WATCH:
			name = "手腕监听设备";
			break;
		case BluetoothClass.Device.TOY_CONTROLLER:
			name = "可穿戴设备";
			break;
		case BluetoothClass.Device.TOY_DOLL_ACTION_FIGURE:
			name = "玩具doll_action_figure";
			break;
		case BluetoothClass.Device.TOY_GAME:
			name = "游戏";
			break;
		case BluetoothClass.Device.TOY_ROBOT:
			name = "玩具遥控器";
			break;
		case BluetoothClass.Device.TOY_UNCATEGORIZED:
			name = "玩具未知设备";
			break;
		case BluetoothClass.Device.TOY_VEHICLE:
			name = "vehicle";
			break;
		case BluetoothClass.Device.HEALTH_BLOOD_PRESSURE:
			name = "健康状态-血压";
			break;
		case BluetoothClass.Device.HEALTH_DATA_DISPLAY:
			name = "健康状态数据";
			break;
		case BluetoothClass.Device.HEALTH_GLUCOSE:
			name = "健康状态葡萄糖";
			break;
		case BluetoothClass.Device.HEALTH_PULSE_OXIMETER:
			name = "健康状态脉搏血氧计";
			break;
		case BluetoothClass.Device.HEALTH_PULSE_RATE:
			name = "健康状态脉搏速率";
			break;
		case BluetoothClass.Device.HEALTH_THERMOMETER:
			name = "健康状态体温计";
			break;
		case BluetoothClass.Device.HEALTH_WEIGHING:
			name = "健康状态体重";
			break;
		case BluetoothClass.Device.HEALTH_UNCATEGORIZED:
			name = "未知健康状态设备";
			break;
		case BluetoothClass.Device.COMPUTER_DESKTOP:
			name = "电脑桌面";
			break;
		case BluetoothClass.Device.COMPUTER_HANDHELD_PC_PDA:
			name = "手提电脑或Pad";
			break;
		case BluetoothClass.Device.COMPUTER_LAPTOP:
			name = "便携式电脑";
			break;
		case BluetoothClass.Device.COMPUTER_PALM_SIZE_PC_PDA:
			name = "微型电脑";
			break;
		case BluetoothClass.Device.COMPUTER_SERVER:
			name = "电脑服务";
			break;
		case BluetoothClass.Device.COMPUTER_UNCATEGORIZED:
			name = "未知的电脑设备";
			break;
		case BluetoothClass.Device.COMPUTER_WEARABLE:
			name = "可穿戴的电脑";
			break;
		}
		return name;
	}
}
